<%--
  Created by IntelliJ IDEA.
  User: yoka24443
  Date: 2018-5-16
  Time: 19:14
  To change this template use File | Settings | File Templates.
--%>
<%@ page import="java.sql.*" contentType="text/html;charset=UTF-8" language="java" %>
<%
    Connection conn = null;
    Statement statement = null;

    StringBuilder output = new StringBuilder();
    String studentNO = request.getParameter("studentNO");
    String realName = new String(request.getParameter("realName").getBytes("ISO-8859-1"), "utf-8");
    String className = new String(request.getParameter("className").getBytes("ISO-8859-1"), "utf-8");
    Integer age = Integer.parseInt(request.getParameter("age"));
    String tel = request.getParameter("tel");
    String address = new String(request.getParameter("address").getBytes("ISO-8859-1"), "utf-8");
    String wechat = new String(request.getParameter("wechat").getBytes("ISO-8859-1"), "utf-8");
    String qq = new String(request.getParameter("qq").getBytes("ISO-8859-1"), "utf-8");

    try {
        //1.加载驱动
        Class.forName("com.mysql.jdbc.Driver");
        //2.驱动管理器获取数据库链接
        conn = DriverManager.getConnection("jdbc:mysql://127.0.0.1:3306/school?user=root&password=&useUnicode=true&characterEncoding=utf8");
        //3.获取statement，执行sql
        statement = conn.createStatement();

        String sql;

        sql = String.format("insert into student values('%s','%s','%s',%s,'%s','%s','%s','%s')",
                studentNO, realName, className, age, tel, address, wechat, qq);
        //System.out.println(sql);
        if (sql.length() > 0) {
            //4.执行sql返回结果集并解析
//            out.println(sql.toString());
            statement.executeUpdate(sql);
            output.append("学号：").append(studentNO).append("<br/>");
            output.append("姓名：").append(realName).append("<br/>");
            output.append("班级名称：").append(className).append("<br/>");
            output.append("年龄：").append(age).append("<br/>");
            output.append("电话：").append(tel).append("<br/>");
            output.append("地址：").append(address).append("<br/>");
            output.append("微信：").append(wechat).append("<br/>");
            output.append("QQ：").append(qq).append("<br/>");
        }
    } catch (ClassNotFoundException e) {
        out.println("连接数据库失败！<br/>" + e.getMessage());
    } catch (SQLException e) {
        out.println("SQL执行出错！<br/>" + e.getMessage());
    } catch (Exception e) {
        out.println(e.getMessage());
    } finally {
        try {
            if (statement != null) {
                statement.close();
            }
            if(conn != null) {
                conn.close();
            }
        } catch (Exception e) {
            out.println(e.getMessage());
        }
    }
%>
<html>
<head>
    <title>add student</title>
</head>
<body>
    <a href="StudentList.jsp">返回</a>
    <p>
        <h3>学生&nbsp;<font color="blue"><%=studentNO%></font>&nbsp;的信息添加成功后，如下：</h3>
        <%=output.toString()%>
    </p>
</body>
</html>
